/*
 * Copyright (C) 2018 Pingan, Inc. All Rights Reserved.
 */
package com.pingan.haofang.searchcloud.dashboard.service;

import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
import org.apache.http.StatusLine;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URISyntaxException;
import java.util.LinkedList;

/**
 * @author baojimin518
 * @version 1.0
 * @create 2018/8/6 10:09
 */
public class CollectionClearUtil {

    static Logger LOG = LoggerFactory.getLogger(CollectionClearUtil.class);

    private static final String domain_st = "http://10.59.72.71:8001";
    private static final String action_st = "/solr/admin/collections?action=DELETE&name=";


    public static void main(String... args) {
        LinkedList<String> list = new LinkedList();

        try (
                FileInputStream fis = new FileInputStream(new File("D:\\collection.txt"));
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(fis, "utf-8"));
        ) {
            String line = null;
            while ((line = bufferedReader.readLine()) != null) {
                if (line.contains(".")) {
                    if (list.size() > 0) {
                        list.pop();
                    }
                    doGet(list);
                    list.removeAll(list);
                    continue;
                }
                list.push(line);
            }
            doGet(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void doGet(LinkedList<String> nameList) throws URISyntaxException, IOException {
        if (nameList != null && nameList.size() > 0) {
            for (String name : nameList) {
                String res = HttpUtil.doGet(domain_st + action_st + name);
                LOG.info("URI :{},result :{}", domain_st + action_st + name, res);
            }
        }
    }


}


/**
 * HttpClient工具类
 */
class HttpUtil {

    private static Logger logger = LoggerFactory.getLogger(HttpUtil.class);

    static CloseableHttpClient httpCilent = HttpClients.createDefault();//Creates CloseableHttpClient instance with default configuration.

    /**
     * get请求
     *
     * @return
     */
    public static String doGet(String url) throws IOException {
        HttpGet httpGet = new HttpGet(url);
        try {
            CloseableHttpResponse execute = httpCilent.execute(httpGet);
            if (execute.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                return EntityUtils.toString(execute.getEntity());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "error";
    }


    /**
     * post请求（用于请求json格式的参数）
     *
     * @param url
     * @param params
     * @return
     */
    public static String doPost(String url, String params) throws Exception {

        HttpPost httpPost = new HttpPost(url);// 创建httpPost
        httpPost.setHeader("Accept", "application/json");
        httpPost.setHeader("Content-Type", "application/json");
        String charSet = "UTF-8";
        StringEntity entity = new StringEntity(params, charSet);
        httpPost.setEntity(entity);
        CloseableHttpResponse response = null;

        try {

            response = httpCilent.execute(httpPost);
            StatusLine status = response.getStatusLine();
            int state = status.getStatusCode();
            if (state == HttpStatus.SC_OK) {
                HttpEntity responseEntity = response.getEntity();
                String jsonString = EntityUtils.toString(responseEntity);
                return jsonString;
            } else {
                logger.error("请求返回:" + state + "(" + url + ")");
            }
        }catch (Exception e){
            e.printStackTrace();
            System.out.println(e.getMessage());
        }
        return null;
    }

}

